package es.juanc.xml;

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class ValidateXmlByXsd2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// Constantes para validacion de Schemas
		final String MY_SCHEMA = "xml/validateXML.xsd";
		final String MY_XML = "xml/validateXML.xml";

		try {

			SchemaFactory factory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
			Schema schema = factory.newSchema( new File( MY_SCHEMA ) );
			Validator validator = schema.newValidator();
			
			validator.validate( new StreamSource( new File( MY_XML ) ) );
			
			System.out.println("El documento está bien formado y es válido");
			
		} catch (SAXException saxEx) {
			System.out.println("SAXException: La cagamos");
		} catch (IOException e) {
			System.out.println("IOException: La cagamos");
		}
	}
}